package flib;

import importExport.SVGLoader;
import importExport.XmlInOut;

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.File;
import java.util.Vector;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import processing.core.PApplet;
import simpleGeom.Bounds3D;
import simpleGeom.Node;
import simpleGeom.Polygon2D;

public class StyleLearn extends PApplet {
	Vector<FlibGraph> groups=new Vector<FlibGraph>();
	String filePath="/Users/administrator/Desktop/splits.svg";
	public void setup() {
		size(800,600);
		loadFloorplan(new File(filePath));
		
	}

	public void draw() {
		//println(groups.size());
		float scale=50;
		strokeWeight(0.01f);
		translate(width*0.5f,height*0.5f);
		rectMode(CENTER);
		for (int i=0;i<1;i++){
			FlibGraph group=groups.get(i);
			Bounds3D b3d=group.getBounds3D();
			pushMatrix();
			
			translate(-b3d.getCenterX(),-b3d.getCenterY());
			
			scale(scale);
			
			for (int j=0;j<group.walls.size();j++){
				FlibWall wall=group.walls.get(j);
				wall.display2D(g);
			}
			for (int j=0;j<group.nodes.size();j++){
				FlibHENode node=group.nodes.get(j);
				if (node.getHEdges().size()>2){
					fill(0,255,255);
				}
				else{
					fill(255);
				}
				rect(node.x,node.y,0.2f,0.2f);
			}
			popMatrix();
		}
	}

	public void loadFloorplan(File file) {

		groups=new Vector<FlibGraph>();
		Document doc = XmlInOut.loadDoc(file);
		println(file);
		NodeList xmlLayers = doc.getElementsByTagName("layer");
		
		
		for (int i = 0; i <xmlLayers.getLength(); i++) {
			FlibGraph group=new FlibGraph();
			Element xmlLayer = (Element) xmlLayers.item(i);
			NodeList xmlLines = xmlLayer.getElementsByTagName("line");
			for (int ii = 0; ii < xmlLines.getLength(); ii++) {
				Element xmlLine = (Element) xmlLines.item(ii);
				Line2D line = SVGLoader.getLine2D(xmlLine);
				group.addWall((float) line.getX1(), (float) line.getY1(),
						(float) line.getX2(), (float) line.getY2(), 0.01f);
			}
			group.intersect();
			group.attach();
			group.createRooms();
			groups.add(group);
		}
	}
}
